<?php

namespace app\admin\controller;

use app\admin\model\Designer;
use app\admin\model\Config;
use app\admin\model\Drawing;
use app\admin\model\Substation;


/**
 * 设计师管理
 * Class Cases
 * @package app\admin\controller
 */
class Designers extends Base
{

    //设计师列表
    public function designerList(){
        $keyword = input('keyword');//关键字
        $Designer = new Designer();
        if (input('?table')) {//是否表格请求
            $result["code"] = 0;
            $result['data'] = $Designer->designerList($keyword);
            $result['count'] = $Designer->designerListCount($keyword);
            return json($result);
        }
        return view('designerList',[
            'keyword'   => $keyword,
            'title'     =>  '设计师列表',
        ]);
    }



    //添加编辑设计师
    public function add(){
        $designer_id = input("designer_id/d",0);
        $Designer = new Designer();
        $Config = new Config();
        if(request()->isPost()){
            $data = input("post.");
            $msg = $Designer->checkData($data, [
                ["crm_designer_id", "require", "请绑定设计师"],
                ["designer_name", "require", "设计师名称不能为空"],
                ["photo", "require", "设计师照片不能为空"],
                ["designer_tel", "require", "设计师电话不能为空"],
                ["designer_level", "require", "设计师级别不能为空"],
                ["sort", "require", "排序不能为空"],
                ["substation_id", "require", "请选择发布网站"],
            ]);
            if ($msg === true) {
                if ($Designer->checkPKFieldOnly("crm_designer_id", $data["crm_designer_id"],$designer_id, $data["substation_id"])) {
                    $result["code"] = 1;
                    $result["msg"] = "绑定的设计师已存在";
                } else {
                    if ($Designer->checkSave($designer_id,$data["substation_id"])) {
                        if ($Designer->mySave($data, "designer_id", $designer_id)) {
                            $result["code"] = 0;
                            $result["msg"] = "操作成功!";
                        } else {
                            $result["code"] = 10000;
                            $result["msg"] = "操作失败！";
                        }
                    }else{
                        $result["code"] = 20000;
                        $result["msg"] = "没有权限操作！";
                    }
                }
            } else {
                $result["code"] = 1;
                $result["msg"] = $msg;
            }
        }else{
            if(!empty($designer_id)) {
                $dataInfo = $Designer->PkInfo($designer_id);//获取库案例信息
                $this->assign("dataInfo", $dataInfo);
            }
            $Substation = new Substation();//分站类
            $userSubstationList = $Substation->userList();//获取分站集合
            $this->assign('userSubstationList',$userSubstationList);
        }
        return !empty($result) ? json($result) : view('add',[
            'title'                 =>  '添加编辑设计师',
            'RadioIs'               =>  config("IsNoRadio"),//获取Radio配置
            'DesignerLevel'         => $Config->getConfigTypeList("DesignerLevel"),
            'ueditor'               =>  true,
        ]);
    }

    //删除设计师
    public function del(){
        $designer_id = input("designer_id/d",0);
        $Designer = new Designer();
        if(!empty($designer_id)){
            if ($Designer->checkSave($designer_id,0)) {
                if ($Designer->PKDel($designer_id, true)) {
                    $result["code"] = 0;
                    $result["msg"] = "操作成功!";
                } else {
                    $result["code"] = 10000;
                    $result["msg"] = "操作失败！";
                }
            }else{
                $result["code"] = 20000;
                $result["msg"] = "没有权限操作！";
            }
        }else{
            $result["code"] = 1;
            $result["msg"] = "designer_id，参数为空!";
        }
        return json($result);
    }

    //选择代表作品
    public function choiceDrawing(){
        $keyword = input('keyword');//关键字
        if (input('?table')) {//是否表格请求
            $Drawing = new Drawing();
            $result['code'] = 0;
            $result['data'] = $Drawing->drawingList($keyword,"","");
            $result['count'] = $Drawing->drawingListCount($keyword,"","");
            return json($result);
        }
        $Config = new Config();
        return view('choiceDrawing',[
            'keyword'           => $keyword,
            'DrawingProgramme'  => $Config->getConfigTypeListArray("DrawingProgramme"),
            'DrawingType'       => $Config->getConfigTypeListArray("DrawingType"),
            'title'             =>  '选择图纸',
        ]);
    }

    //绑定设计师
    public function bindingDesigner(){
        $keyword = input('keyword');//关键字
        if (input('?table')) {//是否表格请求
            $ImgApiData = config("ImgApiData");
            $result = file_get_contents(url($ImgApiData["ApiList"]["designerList"], [
                'token' => md5(md5($ImgApiData["key"]).date("Y-m-d")),
                'keyword'  =>  $keyword
            ], true, $ImgApiData["domain"]));
            $result = json_decode($result,true);
            $result["code"] = 0;
            return json($result);
        }
        return view('bindingDesigner',[
            'title'             =>  '选择图纸',
        ]);
    }
}
